package com.google.project.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.project.client.Service.DailyReportDetailService;
import com.google.project.server.entity.DailyReport;
import com.google.project.server.entity.DailyReportDetail;
import com.google.project.shared.CommonFuntion;
import com.google.project.shared.DailyReportDetailDTO;
import com.google.project.shared.ProjectTaskDTO;

@SuppressWarnings("serial")
public class DailyReportDetailServiceImpl extends RemoteServiceServlet
		implements DailyReportDetailService {

	@Override
	public DailyReportDetailDTO addDailyReportDetail(
			DailyReportDetailDTO dailyReportDetailDTO, String dailyReportId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		DailyReportDetail dailyReportDetail = null;
		if (dailyReportDetailDTO.getId() != null
				&& !dailyReportDetailDTO.getId().equalsIgnoreCase("")) {
			// updateSubConstructor(dailyReportDTO);

		} else {
			try {
				DailyReport dailyReport = pm.getObjectById(DailyReport.class,
						dailyReportId);
				dailyReportDetail = new DailyReportDetail(dailyReportDetailDTO);
				dailyReport.getListOfReportDetail().add(dailyReportDetail);
				pm.makePersistent(dailyReport);
			} finally {
				pm.close();
			}
		}
		return dailyReportDetailDTO;
	}

	@Override
	public List<DailyReportDetailDTO> getDailyReportDetailByDaily(
			String dailyReportId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		DailyReport dailyReportInfo=null;
		List<DailyReportDetailDTO> list = new ArrayList<DailyReportDetailDTO>();
		try {
			dailyReportInfo = pm
					.getObjectById(DailyReport.class, dailyReportId);
			if (dailyReportInfo.getListOfReportDetail() != null) {
				for (int i = 0; i < dailyReportInfo.getListOfReportDetail()
						.size(); i++) {
					list.add(dailyReportInfo.getListOfReportDetail().get(i)
							.toDTO());
				}
			}
		}catch(Exception ex){
			ex.fillInStackTrace();
		} finally {
			pm.close();
		}

		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DailyReportDetailDTO> getDailyReportDetailByTask(String taskId) 
	{
		
		List<DailyReportDetailDTO> listDTO = new ArrayList<DailyReportDetailDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			String whereString = "taskId==\"" + taskId + "\"";
			Query q = pm.newQuery(DailyReportDetail.class, whereString);
			List<DailyReportDetail> list = (List<DailyReportDetail>) q.execute();
			for (DailyReportDetail report : list) {
				listDTO.add(report.toDTO());
			}

		} catch (Exception ex) {
		} finally {
			pm.close();
		}

		return listDTO;
	}
	
	public List<DailyReportDetailDTO> getDailyReportDetailByTaskAndDate(String taskId,Date date) 
	{
		Long dateData = CommonFuntion.removeTime(date);
		
		List<DailyReportDetailDTO> listDTO = new ArrayList<DailyReportDetailDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			String whereString = "taskId==\"" + taskId + "\"" +"&&";
			Query q = pm.newQuery(DailyReportDetail.class, whereString);
			List<DailyReportDetail> list = (List<DailyReportDetail>) q.execute();
			for (DailyReportDetail report : list) {
				listDTO.add(report.toDTO());
			}

		} catch (Exception ex) {
		} finally {
			pm.close();
		}

		return listDTO;
	}
	//Pm Update reportDetail
	@Override
	public void updateReportDetail(DailyReportDetailDTO reportDetailDTO,
			Double work) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		DailyReportDetail reportDetail = null;
		try {
			//update bản thân reportDetail
			reportDetail = pm
					.getObjectById(DailyReportDetail.class, reportDetailDTO.getId());
			
			//update task liên quan
			ProjTaskServiceImpl prjTaskSv=new ProjTaskServiceImpl();
			ProjectServiceImpl projectSv=new ProjectServiceImpl();
			ProjectTaskDTO subTask= prjTaskSv.getTaskById(reportDetailDTO.getTaskId());
			List<ProjectTaskDTO> listTask = projectSv.getListTask(reportDetailDTO.getProjectId());
			subTask.setFinishedWork(subTask.getFinishedWork()+work-reportDetailDTO.getWorkToFinished());
			prjTaskSv.updateTask(subTask);
			prjTaskSv.updateWorkOfParent(subTask, work-reportDetailDTO.getWorkToFinished(), listTask);
			
			reportDetailDTO.setWorkToFinished(work);
			reportDetail.updateFromDTO(reportDetailDTO);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
		
	}
	
}
